<?php

namespace app\admin\controller\choi;

use app\common\controller\Backend;
use think\Db;
use think\exception\PDOException;
use app\admin\model\choi\TeacherCourse as TeacherCourseModel;

/**
 * 教师管理
 *
 * @icon fa fa-circle-o
 */
class Teacher extends Backend
{
    
    /**
     * Teacher模型对象
     * @var \app\admin\model\choi\Teacher
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\choi\Teacher;
        $this->view->assign("sexList", $this->model->getSexList());
        $this->view->assign("professionList", $this->model->getProfessionList());
        $this->view->assign("verifyList", $this->model->getVerifyList());
        $this->view->assign("gradeList", $this->model->getGradeList());
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->with(['images'])
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->with(['images'])
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $list = collection($list)->toArray();
            foreach ($list as &$val) {
                $images = $val['images'] ? array_column($val['images'], 'image') : '';
                $val['images'] = $images;
                $teachingYear = 0;
                if ($val['year']) {
                    $year = date('Y') - $val['year'];
                    $teachingYear = $year > 1 ? $year : 1;
                }
                $val['teaching_year'] = $teachingYear;
            }
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }


    /**
     * 审核身份证
     */
    public function verify($id = 0)
    {
        $teacher = $this->model->find($id);
        if ($this->request->isPost()) {
            $verify = $this->request->post('verify');
            $teacher->verify = $verify;
            $res = $teacher->save();
            $res ? $this->success('您的授课认证审核已通过！') : $this->error('您的授课认证审核未通过！');
        }
        return $this->fetch();
    }

    /**
     * 等级
     */
    public function grade($id = 0)
    {
        $teacher = $this->model->find($id);
        $gradeList = $this->model->getGradeList();
        if ($this->request->isPost()) {
            $grade = $this->request->post('grade');
            $teacher->grade = $grade;
            $res = $teacher->save();
            $res ? $this->success('您的教学等级已变更为'.$gradeList[$grade]) : $this->error('更改等级失败,请联系管理员');
        }
        $this->assign('gradeList', $gradeList);
        $this->assign('teacher', $teacher);
        return $this->fetch();
    }

    /**
     * 删除
     */
    public function del($ids = "")
    {
        if ($ids) {
            $pk = $this->model->getPk();
            $adminIds = $this->getDataLimitAdminIds();
            if (is_array($adminIds)) {
                $this->model->where($this->dataLimitField, 'in', $adminIds);
            }
            $list = $this->model->where($pk, 'in', $ids)->select();

            $count = 0;
            Db::startTrans();
            try {
                foreach ($list as $k => $v) {
                    $count += $v->delete();
                }
                //下架课程
                TeacherCourseModel::del($ids);

                Db::commit();
            } catch (PDOException $e) {
                Db::rollback();
                $this->error($e->getMessage());
            } catch (Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            if ($count) {
                $this->success();
            } else {
                $this->error(__('No rows were deleted'));
            }
        }
        $this->error(__('Parameter %s can not be empty', 'ids'));
    }

}
